package com.prp.interceptors;

import java.util.Iterator;
import java.util.Set;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.prp.dao.Adminrole;
import com.prp.dao.Userinfo;
import com.prp.service.ServiceFactory;

public class AuthorityInterceptor implements Interceptor {
	
	private String authExisted;
	public String getAuthExisted() {
		return authExisted;
	}

	public void setAuthExisted(String authExisted) {
		this.authExisted = authExisted;
	}

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void init() {
		// TODO Auto-generated method stub

	}

	public String intercept(ActionInvocation arg0) throws Exception {
		String username = (String) ActionContext.getContext().getSession().get("username");
		if(username == null){
			return "login";
		}
		else{
			Set authSet = ((Userinfo)ServiceFactory.getUserManage().getUser(username)).getAdminroles();
			Iterator it = authSet.iterator();
			while(it.hasNext()){
				Adminrole adminrole = (Adminrole)it.next();
				if(authExisted.equals("UserAdmin")){				
					if(adminrole.getId().getRole().getRole() == 1 
							||adminrole.getId().getRole().getRole() == 3)
						return arg0.invoke();
				}
				
				if(authExisted.equals("FileAdmin")){
					if(adminrole.getId().getRole().getRole() == 2 
							||adminrole.getId().getRole().getRole() == 3)
						return arg0.invoke();
				}
			}
		}
		return "login";
	}

}
